package hao.bk.royalmap;

import java.util.ArrayList;

import hao.bk.searchMap.LocalShopAdapter;
import hao.bk.sql.bo.Location;
import hao.bk.sql.dao.LocationDAO;
import hao.bk.utils.CharactersMark;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.AdapterView.OnItemClickListener;

public class SearchFasterGeneralLayout  extends RelativeLayout implements
OnItemClickListener,TextWatcher {
	private RoyalMapActivity main;
	public ListView listSearch;
	private String preSeachedText;
	protected SearchBarLayout searchBarLayout;
	ArrayList<Location> listSearchFaster = new ArrayList<Location>();
	ArrayList<Location> locationListShow = new ArrayList<Location>();
	SearchFasterAdapter adapter = null;
	
	public SearchFasterGeneralLayout(Context context) {
		super(context);
		main = (RoyalMapActivity) context;
		initParams();
		
	}
	public void initParams(){
		inflate(main, R.layout.search_faster, this);
		searchBarLayout = (SearchBarLayout)
				findViewById(R.id.search_faster_searchbar);
		listSearch = (ListView)findViewById(R.id.search_fastter_list_shop);
		listSearch.setOnItemClickListener(this);
		preSeachedText = "";
		fillListSearch();
	}
	public void fillListSearch() {
		ArrayList<Location> list = new ArrayList<Location>();
		LocationDAO locationDAO = new LocationDAO(main);
		list = locationDAO.getLocationInDb();
		for(Location locate : list) {
			listSearchFaster.add(locate);
		}
		adapter = new SearchFasterAdapter(main, R.layout.search_faster_list_item,
				listSearchFaster);
		listSearch.setAdapter(adapter);
		searchBarLayout.textEdt.addTextChangedListener(this);

		searchBarLayout.textEdt.setText("");			
		searchBarLayout.textEdt.setHint("Tìm kiếm nhanh");
		
		
	}

	public void updateSearchLocation() {
		preSeachedText = searchBarLayout.getText();
		String foundStr = CharactersMark.convertFoundString(
				preSeachedText.trim()).toLowerCase();
		locationListShow = new ArrayList<Location>();
		if (foundStr != null && !foundStr.equals("")) {
			searchBarLayout.showClearBtn(true);
			for (int i = 0; i < listSearchFaster.size(); i++) {
				String nameLocation = listSearchFaster.get(i).getName();
				
				String inputStr = CharactersMark.convertFoundString(nameLocation)
						.toLowerCase();
				if (inputStr.contains(foundStr)) {
					locationListShow.add(listSearchFaster.get(i));
				}
			}
		} else {
			locationListShow = listSearchFaster;
			searchBarLayout.showClearBtn(false);
		}
		if(locationListShow.isEmpty()){
			listSearch.setVisibility(GONE);
		
		}else{
			listSearch.setVisibility(VISIBLE);
			listSearch.setAdapter(new SearchFasterAdapter(main, R.layout.search_faster_list_item,
					locationListShow ));
		}
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		updateSearchLocation ();
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// ẩn bàn phím
		main.hiddenKeyboardForView(searchBarLayout.textEdt);
		searchBarLayout.textEdt.clearTextChangedListeners();
		String nameLocation = locationListShow.get(position).getName();
		/** Ẩn tìm kiếm nhanh và hiện bubbles */
		main.searchFaster.setVisibility(View.GONE);
		GeneralAnimationUtils.startTopToBottom(main.searchFaster);
		main.btnMap.setText("Bản đồ");
		main.fasterSearchIsView = false;
		/** Lấy về id area của cửa hàng được click để add bubles */
		int areaId = main.mapB1.getIdAreaViaName(nameLocation+"");
		if(areaId > 0) {
			main.clicked.add(areaId);
			main.mapB1.showBubble(areaId);
			main.mapB1.invalidate();
		} else {
			main.showToastCustom("Địa điểm này chưa có trong Cơ sở dữ liệu ");
		}
		
	}

}
